<template>
  <div>
    <el-autocomplete
      v-model.trim="name"
      ref="company"
      :trigger-on-focus="triggerOnFocus"
      @clear="nicknameClear(), $refs.company.$children[0].blur()"
      :fetch-suggestions="
        (queryString, cb) => {
          basicquerySearchAsync(queryString, cb);
        }
      "
      :placeholder="palceholder"
      clearable
      class="wid220 main_comm"
      :highlight-first-item="true"
      value-key="nickname"
      @select="
        (item) => {
          basicChange(item);
        }
      "
    >
      <template slot-scope="{ item }">
        <div class="main_name">
          <span> {{ item.nickname }}</span>
          <span class="code">{{ item.info ? item.info.code : "" }}</span>
        </div>
      </template>
    </el-autocomplete>
  </div>
</template>

<script>
import { member_getMembers, sort_getMembers } from "@/api/currency";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
export default {
  props: ["uid", ""],
  props: {
    palceholder: {
      type: String,
      default: "用户名称",
    },
    triggerOnFocus: {
      type: Boolean,
      default: false,
    },
    getFunction: {
      type: Number,
      default: 1,
    },
    time: {
      type: String,
      default: null,
    },
    name_value: {
      type: String,
      default: null,
    },
  },
  data() {
    return {
      name: "",
      send_time: "",
    };
  },
  watch: {
    time: {
      handler(val) {
        this.send_time = val;
      },
      immediate: true,
    },
    name_value: {
      handler(val) {
        if (val) {
          this.name = val;
        }
      },
      deep: true,
      immediate: true,
    },
  },
  created() {},
  mounted() {},
  methods: {
    basicquerySearchAsync(queryString, cb) {
      let func = null;
      let form = null;
      switch (this.getFunction) {
        case 1:
          // 获取全部用户
          func = member_getMembers;
          form = {
            name: queryString.trim(),
          };
          break;
        case 2:
          func = sort_getMembers;
          form = {
            name: queryString.trim(),
            send_time: this.send_time,
          };
          break;
        default:
          func = member_getMembers;
          form = {
            name: queryString.trim(),
          };
          break;
      }
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        form
      );
      func(data)
        .then((res) => {
          if (res.errcode == 0) {
            var results = res.data.list;
            cb(results);
          } else {
            this.$message.error(res.errmsg);
            cb([]);
          }
        })
        .catch(() => {
          cb([]);
        });
    },
    basicChange(item) {
      this.$emit("sendValue", item);
      this.$emit("zidname",item.id);
    },
    nicknameClear() {
      this.$emit("clear");
    },
  },
};
</script>

<style lang="scss" scoped>
.main_name {
  display: flex;
  flex-direction: column;
  line-height: initial;
  padding: 8px 0;
  height: 50px;
  .code {
    color: #999;
  }
}
</style>